/*
 * player.c
 *
 *  Created on: Jul 11, 2011
 *      Author: alemao
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "player.h"

typedef int (*parse_function)(char *statistic, player *Player);
static int parse_class(char *statistic, player *Player);
static int parse_name(char *statistic, player *Player);
static int parse_team(char *statistic, player *Player);
static int parse_price(char *statistic, player_statistcs *Statistic);
static int parse_confronto(char *statistic, char *playerteam, player_statistcs *Statistic);

struct table{
	char *name;
	int value;
};

const struct table classes[] = {
		{"ATA", ATACANTE},
		{"GOL", GOLEIRO},
		{"LAT", LATERAL},
		{"MEI", MEIA},
		{"ZAG", ZAGUEIRO},
		{"TEC", TECNICO},
		{NULL, -1}

};

const struct table teams[] = {
		{"américa-mg", AMERICAMG},
		{"atlético-go", ATLETICOGO},
		{"atlético-mg", ATLETICOMG},
		{"atlético-pr", ATLETICOPR},
		{"avaí", AVAI},
		{"bahia", BAHIA},
		{"botafogo", BOTAFOGO},
		{"ceará", CEARA},
		{"corinthians", CORINTHIANS},
		{"coritiba", CORITIBA},
		{"cruzeiro", CRUZEIRO},
		{"figueirense", FIGUEIRENSE},
		{"flamengo", FLAMENGO},
		{"fluminense", FLUMINENSE},
		{"grêmio", GREMIO},
		{"internacional", INTERNACIONAL},
		{"palmeiras", PALMEIRAS},
		{"santos", SANTOS},
		{"são paulo", SAOPAULO},
		{"vasco", VASCO},
		{NULL, -1}
};
const parse_function parse_functions[] = {
		parse_class,
		parse_name,
		parse_team,
		NULL,
};

static int parse_class(char *statistic, player *Player){
	int i;

	for (i=0; classes[i].name != NULL; i++)
		if(!strncmp(classes[i].name, statistic, strlen(classes[i].name)))
			break;
	Player->class = classes[i].value;
	return classes[i].value;
}

static int parse_price(char *statistic, player_statistcs *Statistic){
	int C, c;

	if (sscanf(statistic, "C$ %d.%d", &C, &c) < 2)
		return -1;

	Statistic->PRECO = (C*100) + c;
	return 0;
}

static int parse_team(char *statistic, player *Player){
	int i;

	for (i=0; teams[i].name != NULL; i++)
		if(!strncmp(teams[i].name, statistic, strlen(teams[i].name)))
			break;
	Player->team = teams[i].value;
	return teams[i].value;
}

static int parse_name(char *statistic, player *Player){
	Player->name = malloc(strlen(statistic)+1);
	if (!Player->name)
		return -1;
	if (sscanf(statistic, "%[^\t]", Player->name) )
		return 0;
	return -1;
}

static int parse_float(char *statistic)
{
	int C, c;

	if (sscanf(statistic, "%d.%d", &C, &c) < 2)
		return -1;

	return (C*100) + c;
}

extern player *libplayer_parseplayer(char *player_stats){
	char tmp[2048];
	char *statistic;
	int i;
	player *Player;

	if (!player_stats)
		return NULL;

	if ( !(Player = malloc(sizeof(player))) ){
		return NULL;
	}
	memset(Player, 0, sizeof(player));

	statistic = player_stats;
	i=0;

	do {
		sscanf(statistic, "\t%[^\t]", tmp);
		if (parse_functions[i])
			if (parse_functions[i](tmp, Player) < 0)
				return NULL;
		i++;
		if (!parse_functions[i])
			break;//not implemented
		statistic++;
	}while( (statistic = strchr(statistic, '\t')) );

	return Player;
}
static int parse_confronto(char *statistic, char *playerteam, player_statistcs *Statistic)
{
	char teama[20];
	char teamb[20];
	int i;

	if (!playerteam || !statistic || !Statistic)
		return -1;

	if (sscanf(statistic, "escudo %[^X]Xescudo %[^\t]", teama, teamb) < 2)
		return -1;

	if (!strncmp(playerteam, teama, strlen(teama)))
	{
		for (i=0; teams[i].name != NULL; i++)
			if( !strncmp(teams[i].name, teamb, strlen(teams[i].name)) )
				break;
		Statistic->JOGOU_CONTRA = teams[i].value;
		Statistic->JOGOU_EM_CASA = 1;
	}
	else {
		for (i=0; teams[i].name != NULL; i++)
			if( !strncmp(teams[i].name, teama, strlen(teams[i].name)) )
				break;
		Statistic->JOGOU_CONTRA = teams[i].value;
		Statistic->JOGOU_EM_CASA = 0;
	}
	return 0;

}
extern player_statistcs *libplayer_parsestatistics(char *player_stats){
	player_statistcs *Statistics;
	char tmp[2048];
	char team[20];
	char *current_stat;
	int i, ret;
	int *stat;


	if (!player_stats)
		return NULL;

	if ( !(Statistics = malloc(sizeof(player_statistcs))) ){
		return NULL;
	}

	current_stat = player_stats;
	for (i=0; i<13; i++)
	{
		switch (i){
		case 2:
			sscanf(current_stat, "%[^\t]", team);
			break;
		case 5:
			sscanf(current_stat, "%[^\t]", tmp);
			Statistics->JOGOU = strtol(tmp, NULL, 10);
			break;
		case 6:
			sscanf(current_stat, "%[^\t]", tmp);
			parse_price(tmp, Statistics);
			break;
		case 8:
			sscanf(current_stat, "%[^\t]", tmp);
			Statistics->MEDIA = parse_float(tmp);
			break;
		case 9:
			sscanf(current_stat, "%[^\t]", tmp);
			Statistics->PONTUACAO = parse_float(tmp);
			break;
		case 11:
			sscanf(current_stat, "%[^\t]", tmp);
			strcat(tmp, "\t");
			if (parse_confronto(tmp, team, Statistics))
				printf("parse_confronto error, tmp: %s\n", tmp);
			break;
		}
		current_stat = strchr(current_stat, '\t');
		current_stat++;
	}

	for (i=STATISTICS_OFFSET; i<STATISTICS_ARRAY_SIZE; i++)
	{
		sscanf(current_stat, "%[^\t]", tmp);
		Statistics->stats[i] = strtol(tmp, NULL, 10);
		current_stat = strchr(current_stat,'\t');
		current_stat++;
	}
	return Statistics;
}

extern void libplayer_freeplayer(player *Player){
	int i;
	for (i; i < ROUNDS_TOTAL; i++)
		if (Player->statistics[i])
			free(Player->statistics[i]);
	free(Player->name);
	free(Player);
	Player = NULL;
}

extern void libplayer_freestatistics(player_statistcs *Statistics)
{
	free(Statistics);
}

extern int libplayer_getteamname(char *teamname, int team, size_t size)
{
	int i;
	for (i=0; teams[i].name != NULL; i++)
	{
		if (teams[i].value == team)
		{
			strncpy(teamname, teams[i].name, size);
			return 0;
		}
	}
	return -1;
}

extern int libplayer_getclassname(char *classname, int class, size_t size)
{
	int i;
	for (i=0; classes[i].name != NULL; i++)
	{
		if (classes[i].value == class)
		{
			strncpy(classname, classes[i].name, size);
			return 0;
		}
	}
	return -1;
}

static int calc_round_statistic(player_statistcs *round_before, player_statistcs *round)
{
	int i;
	round->JOGOU = round->JOGOU - round_before->JOGOU;
	round->PRECO = round->PRECO - round_before->PRECO;
	for (i = STATISTICS_OFFSET; i < STATISTICS_ARRAY_SIZE; i++)
		round->stats[i] = round->stats[i] - round_before->stats[i];
}

extern int libplayer_cleanstatistics(player *Player)
{
	int i, j, diff;

	for (i=ROUNDS_TOTAL - 2; i >= 0; i-- )
	{
		if (Player->statistics[i])
		{
			for (j=i+1; j < ROUNDS_TOTAL; j++)
			{
				if (Player->statistics[j])
				{
//					diff = Player->statistics[j]->RODADAS_JOGADAS - Player->statistics[i]->RODADAS_JOGADAS;
//					if (diff == 1)
//					{
						calc_round_statistic(Player->statistics[i], Player->statistics[j]);
						break;
//					}
//					else if (diff == 0)
//					{
//						libplayer_freestatistics(Player->statistics[j]);
//						Player->statistics[j] = NULL;
//						break;
//					}
				}
			}
		}
	}
	return 0;
}
